package com.yunji.web.storage;

import com.github.pagehelper.page.PageMethod;
import com.yunji.aggregate.model.dto.stock.DiscardTicketParam;
import com.yunji.aggregate.model.vo.stock.DiscardTicketStatisticsVO;
import com.yunji.aggregate.service.StockAggregateService;
import com.yunji.base.enums.AuditStatus;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.storage.business.DiscardTicketBusiness;
import com.yunji.storage.dto.DiscardTicketDTO;
import com.yunji.storage.vo.DiscardTicketVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.web.storage
 * @Title: DiscardTicketController
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-24 11:12:10      V1.0        HuaAo       新建类
 */
@Api(tags = "管理端-报废模块")
@RestController
@RequestMapping("storage/discard")
public class DiscardTicketController extends BaseController {

    @Resource
    DiscardTicketBusiness business;

    @Resource
    StockAggregateService aggregateService;

    /**
     * @param param
     * @Title export
     * @Desc 导出报废单
     * @Date 2024-10-10 14:51:50.036
     */
    @ApiOperation("导出报废单")
    @PostMapping("/export")
//    @PreAuthorize("@ss.hasPermi('inventory:obsolescence:export')")
    public void export(DiscardTicketParam param) {
        List<DiscardTicketStatisticsVO> list = aggregateService.searchDiscardList(param);
        ExcelUtil<DiscardTicketStatisticsVO> util = new ExcelUtil<>(DiscardTicketStatisticsVO.class);
        util.exportExcel(response, list, "报废单数据");
    }

    /**
     * 报废单列表
     *
     * @param param
     * @return
     */
    @ApiOperation("报废单列表")
    @PostMapping("/list")
    public Response<List<DiscardTicketStatisticsVO>> list(@RequestBody DiscardTicketParam param) {
        PageMethod.startPage(param.getPageNum(), param.getPageSize());
        List<DiscardTicketStatisticsVO> list = aggregateService.searchDiscardList(param);
        return ResponseHelper.successWithPage(list);
    }

    /**
     * 新增报废单
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增报废单")
    @PostMapping
//    @PreAuthorize("@ss.hasPermi('inventory:obsolescence:add')")
    public Response<Long> create(@RequestBody DiscardTicketDTO dto) {
        return ResponseHelper.success(business.create(dto));
    }

    /**
     * 报废单详情
     *
     * @param discardId
     * @return
     */
    @ApiOperation("报废单详情")
    @GetMapping("/{discardId}")
    public Response<DiscardTicketVO> getInfo(@PathVariable Long discardId) {
        return ResponseHelper.success(business.select(discardId));
    }

    /**
     * 保存草稿
     *
     * @param
     * @return
     */
    @ApiOperation("保存草稿")
    @PostMapping("/saveDraft")
    public Response<String> saveDraft(@RequestBody DiscardTicketDTO dto) {
        business.saveDraft(dto);
        return ResponseHelper.success("保存成功");
    }

    /**
     * 获取草稿
     *
     * @param belongId
     * @return
     */
    @ApiOperation("获取草稿")
    @GetMapping("/getDraft")
    public Response<DiscardTicketVO> getDraft(@RequestParam Long belongId) {
        return ResponseHelper.success(business.getDraft(belongId));
    }

    /**
     * 审核通过
     *
     * @param discardId
     * @return
     */
    @ApiOperation("审核通过")
    @GetMapping("/acceptAudit")
    public Response<String> acceptAudit(@RequestParam(name = "discardId") Long discardId) {
        business.audit(discardId, AuditStatus.ACCEPT.getCode(), null);
        return ResponseHelper.success("提交成功");
    }

    /**
     * 审核驳回
     *
     * @param discardId
     * @param reason
     * @return
     */
    @ApiOperation("审核驳回")
    @GetMapping("/rejectAudit")
    public Response<String> rejectAudit(@RequestParam(name = "discardId") Long discardId, @RequestParam(name = "reason") String reason) {
        business.audit(discardId, AuditStatus.REJECT.getCode(), reason);
        return ResponseHelper.success("提交成功");
    }

    /**
     * 取消报废单
     *
     * @param discardId
     * @return
     */
    @ApiOperation("取消报废单")
    @PutMapping("/cancel")
    public Response<String> cancel(@RequestParam Long discardId) {
        business.cancel(discardId);
        return ResponseHelper.success("取消成功");
    }

    /**
     * 重新提交
     *
     * @param dto
     * @return
     */
    @ApiOperation("重新提交")
    @PostMapping("/commit")
    public Response<String> commit(@RequestBody DiscardTicketDTO dto) {
        business.commit(dto);
        return ResponseHelper.success("提交成功");
    }

    /**
     * 批量删除报废单
     *
     * @return
     */
    @ApiOperation("删除报废单")
    @DeleteMapping("/{discardIds}")
//    @PreAuthorize("@ss.hasPermi('inventory:obsolescence:remove')")
    public Response<String> delete(@PathVariable Long... discardIds) {
        business.delete(discardIds);
        return ResponseHelper.success("删除成功");
    }

}